package com.qdc.datacenters.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qdc.datacenters.domain.entity.BaseEntity;
import com.qdc.datacenters.domain.entity.SysOperLog;
import com.qdc.datacenters.domain.query.BaseQuery;
import com.qdc.datacenters.domain.query.log.SysOperationLogQuery;
import com.qdc.datacenters.enums.DeleteStatusEnum;
import com.qdc.datacenters.exception.CommonException;
import com.qdc.datacenters.mapper.SysOperLogMapper;
import com.qdc.datacenters.service.ISysOperLogService;
import com.qdc.datacenters.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统操作日志记录(SysOperLog)表服务实现类
 *
 * @author danmo
 * @since 2024-03-12 19:09:58
 */
@Service
public class SysOperLogServiceImpl extends BaseServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {

    @Override
    public List<SysOperLog> getList(SysOperationLogQuery query) {
        return this.baseMapper.getList(query);
    }

    @Override
    public void delete(SysOperationLogQuery query) {
        if (CollectionUtil.isEmpty(query.getIds())) {
            throw new CommonException("ID不能为空");
        }
        update(new LambdaUpdateWrapper<SysOperLog>()
                .set(SysOperLog::getDelFlag, DeleteStatusEnum.DELETE_YES.getIndex())
                .set(BaseEntity::getUpdateBy, SecurityUtils.getUserId())
                .set(BaseEntity::getUpdateTime, new Date())
                .in(SysOperLog::getId, query.getIds()));
    }

    @Override
    public void emptyOperation(BaseQuery query) {
        remove(new LambdaQueryWrapper<SysOperLog>().gt(SysOperLog::getId,0));
    }
}

